package com.ruoyi.client.controller;

import com.ruoyi.client.domain.vo.request.ClientAddressAddRequestVo;
import com.ruoyi.client.domain.vo.request.ClientAddressDeleteRequestVo;
import com.ruoyi.client.domain.vo.request.ClientAddressSetDefaultRequestVo;
import com.ruoyi.client.domain.vo.request.ClientAddressUpdateRequestVo;
import com.ruoyi.client.service.IUserShippingAddressService;
import com.ruoyi.client.util.ResponseResult;
import com.ruoyi.common.core.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户收货地址Controller
 * 
 * @author ruoyi
 * @date 2024-02-24
 */
@RestController
@RequestMapping("/client/address")
public class ClientAddressController extends BaseController {
    @Autowired
    private IUserShippingAddressService addressService;

    /**
     * 获取用户收货地址列表
     */
    @GetMapping("/list")
    public ResponseResult getAddressList(@RequestParam("userId") String userId) {
        return ResponseResult.success(addressService.getUserAddressList(userId));
    }
    
    /**
     * 获取收货地址详情
     */
    @GetMapping("/{id}")
    public ResponseResult getAddressDetail(@PathVariable("id") Long id) {
        return ResponseResult.success(addressService.getUserAddressById(id));
    }
    
    /**
     * 获取默认收货地址
     */
    @GetMapping("/default")
    public ResponseResult getDefaultAddress(@RequestParam("userId") String userId) {
        return ResponseResult.success(addressService.getDefaultAddress(userId));
    }
    
    /**
     * 新增收货地址
     */
    @PostMapping("/add")
    public ResponseResult addAddress(@RequestBody ClientAddressAddRequestVo requestVo) {
        // 参数校验
        if (requestVo.getReceiver() == null || requestVo.getReceiver().length() < 2 || requestVo.getReceiver().length() > 20) {
            return ResponseResult.fail("收货人姓名长度应为2-20个字符");
        }
        if (requestVo.getPhone() == null || !requestVo.getPhone().matches("^1[3-9]\\d{9}$")) {
            return ResponseResult.fail("请输入正确的手机号码");
        }
        if (requestVo.getDetailAddr() == null || requestVo.getDetailAddr().length() < 5 || requestVo.getDetailAddr().length() > 200) {
            return ResponseResult.fail("详细地址长度应为5-200个字符");
        }
        
        int result = addressService.addAddress(requestVo);
        if (result == -1) {
            return ResponseResult.fail("最多可添加10个收货地址");
        } else if (result > 0) {
            return ResponseResult.success("添加成功");
        } else {
            return ResponseResult.fail("添加失败");
        }
    }
    
    /**
     * 修改收货地址
     */
    @PutMapping("/update")
    public ResponseResult updateAddress(@RequestBody ClientAddressUpdateRequestVo requestVo) {
        // 参数校验
        if (requestVo.getId() == null) {
            return ResponseResult.fail("地址ID不能为空");
        }
        if (requestVo.getReceiver() != null && (requestVo.getReceiver().length() < 2 || requestVo.getReceiver().length() > 20)) {
            return ResponseResult.fail("收货人姓名长度应为2-20个字符");
        }
        if (requestVo.getPhone() != null && !requestVo.getPhone().matches("^1[3-9]\\d{9}$")) {
            return ResponseResult.fail("请输入正确的手机号码");
        }
        if (requestVo.getDetailAddr() != null && (requestVo.getDetailAddr().length() < 5 || requestVo.getDetailAddr().length() > 200)) {
            return ResponseResult.fail("详细地址长度应为5-200个字符");
        }
        
        int result = addressService.updateAddress(requestVo);
        if (result > 0) {
            return ResponseResult.success("修改成功");
        } else {
            return ResponseResult.fail("修改失败");
        }
    }
    
    /**
     * 删除收货地址
     */
    @PostMapping("/delete")
    public ResponseResult deleteAddress(@RequestBody ClientAddressDeleteRequestVo requestVo) {
        if (requestVo.getId() == null) {
            return ResponseResult.fail("地址ID不能为空");
        }
        
        int result = addressService.deleteAddress(requestVo);
        if (result > 0) {
            return ResponseResult.success("删除成功");
        } else {
            return ResponseResult.fail("删除失败");
        }
    }
    
    /**
     * 设置默认收货地址
     */
    @PutMapping("/set-default")
    public ResponseResult setDefaultAddress(@RequestBody ClientAddressSetDefaultRequestVo requestVo) {
        if (requestVo.getId() == null) {
            return ResponseResult.fail("地址ID不能为空");
        }
        
        int result = addressService.setDefaultAddress(requestVo);
        if (result > 0) {
            return ResponseResult.success("设置成功");
        } else {
            return ResponseResult.fail("设置失败");
        }
    }
} 